package com.teach_sys.serlvet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.teach_sys.bean.Course;
import com.teach_sys.service.CourseService;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "CourseServlet", value = "/CourseServlet")
public class CourseServlet extends HttpServlet {

    private final CourseService courseService = new CourseService();

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (request.getParameter("action").equals("queryAllCourse")) {
            queryAllCourse(response);
        } else if (request.getParameter("action").equals("queryCourseByCno")) {
            queryCourseByCno(request, response);
        } else if (request.getParameter("action").equals("queryCourseByCname")) {
            queryCourseByCname(request, response);
        } else if (request.getParameter("action").equals("addCourse")) {
            addCourse(request, response);
        } else if (request.getParameter("action").equals("deleteCourse")) {
            deleteCourse(request, response);
        } else if (request.getParameter("action").equals("updateCourse")) {
            updateCourse(request, response);
        }
    }

    public void queryAllCourse(HttpServletResponse response) throws IOException {
        List<Course> courseList = courseService.queryAllCourse();
        String result = JSON.toJSONString(courseList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void queryCourseByCno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cno = request.getParameter("Cno");
        List<Course> courseList = courseService.queryCourseByCno(cno);
        String result = JSON.toJSONString(courseList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void queryCourseByCname(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cname = request.getParameter("Cname");
        List<Course> courseList = courseService.queryCourseByCname(cname);
        String result = JSON.toJSONString(courseList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void addCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Course course = JSON.parseObject(request.getParameter("formObject"), Course.class);
        courseService.addCourse(course);
    }

    public void deleteCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cno = request.getParameter("Cno");
        JSONArray jsonArray = JSON.parseArray(cno);
        List<String> deleteList = jsonArray.toJavaList(String.class);
        for (String i : deleteList) {
            courseService.deleteCourseByCno(i);
        }
    }

    public void updateCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Course course = JSON.parseObject(request.getParameter("formObject"), Course.class);
        courseService.updateCourse(course);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void destroy() {
    }
}
